<?php
/**
 * @author fld 2021-06-18
 * @desc 后台菜单控制类
 */
namespace App\Http\Controllers\Admin;

use App\Service\Admin\MenuService;
use Illuminate\Http\Request;

class MenuController extends AdminBaseController
{
    /**
     * @author fld 2021-06-17
     * @desc 角色列表
     * @param Request $request
     * @param MenuService $menuService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request,MenuService $menuService){
        $list = $menuService->getMenuList();
        return view('admin/menu/menu_index', ['list'=>json_encode($list)]);
    }

    /**
     * @author fld 2021-06-18
     * @desc 新增角色信息
     * @param Request $request
     * @param MenuService $menuService
     * @param MenuService $menuService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function add(Request $request,MenuService $menuService){
        if($request->isMethod('post')){
            $result = $menuService->addOrUpdateMenu();
            if($result['code'] == 0){
                return $this->success(['url'=>url('admin/menu/index'),'message'=>$result['message']]);
            }else{
                return $this->failed($result['code'], ['message'=>$result['message']]);
            }
        }else {
            $data = [];
            $menu_list = $menuService->getAllMenuList();
            $data['menu_list'] = $menu_list;
            return view('admin/menu/menu_add', $data);
        }
    }

    /**
     * @author fld 2021-06-18
     * @desc 修改角色信息
     * @param Request $request
     * @param MenuService $menuService
     * @param MenuService $menuService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request,MenuService $menuService){
        if($request->isMethod('post')){
            $result = $menuService->addOrUpdateMenu();
            if($result['code'] == 0){
                return $this->success(['url'=>url('admin/menu/index'),'message'=>$result['message']]);
            }else{
                return $this->failed($result['code'], ['message'=>$result['message']]);
            }
        }else{
            $data = [];
            $id = $request->get('id');

            $item = $menuService->getMenuById($id);
            $data['item'] = $item;

            $menu_list = $menuService->getAllMenuList();
            $data['menu_list'] = $menu_list;
            return view('admin/menu/menu_edit', $data);
        }
    }

    /**
     * @author fld 2021-06-18
     * @desc 删除角色信息
     * @param MenuService $menuService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(MenuService $menuService){
        $id = request()->post('id');
        $result = $menuService->deleteMenuById($id);
        if($result['code'] == 0){
            return $this->success(['url'=>url('admin/menu/index'),'message'=>$result['message']]);
        }else{
            return $this->failed($result['code'], ['message'=>$result['message']]);
        }
    }

    public function update_status(MenuService $menuService){
        $result = $menuService->update_status();
        if($result['code'] == 0){
            return $this->success(['url'=>url('admin/menu/index'),'message'=>$result['message']]);
        }else{
            return $this->failed($result['code'], ['message'=>$result['message']]);
        }
    }
}
